// pages/personal-balance/personal-balance.js

import api from '../../utils/request/api'
const util = require('../../utils/util.js');
import {
  userBalance
} from '../../utils/request/apiurl'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userData: "",
    uid: "", 
    balanceData: {},//余额数据
	isRequest: false,
    searchGood: {
    	page: 1,
    	size: 10,
      pages: 0,
	 }, 
    // uid: "da3bdf61cc1a409cafe573ea389c0e09", //测试uid
    dataList: [],
	isColonel:0,//0不是团长，1是团长
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBanlanceF();//获取余额
    this.getBalanceList()
  },
  //获取余额
    getBanlanceF(){
      this.setData({
        uid: wx.getStorageSync('userInfo').uid,
        // role: wx.getStorageSync('userInfo').role
      })
      // 获取我的余额
      this.setData({
        isColonel: wx.getStorageSync('userInfo').isColonel
      })
      // console.log(wx.getStorageSync('userInfo'))
      api.post(userBalance.balance, {
        uid: this.data.uid || wx.getStorageSync('userInfo').uid
      }).then(res => {
        // console.log(res)
        this.setData({
          // balance: res || 0
          balanceData: res,
        })
      }).catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none'
        })
      })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	this.setData({
		uid: wx.getStorageSync('userInfo').uid,
		userData:wx.getStorageSync('Info')
		// role: wx.getStorageSync('userInfo').role
	})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
  	wx.showNavigationBarLoading() //在标题栏中显示加载
	this.setData({
		dataList: [],
		searchGood: {
			page: 1,
			size: 10,
		  pages: 0,
		 },
    })
    this.getBanlanceF();//获取余额
  	this.getBalanceList()
  	setTimeout(function() {
  		wx.hideNavigationBarLoading() //完成停止加载
  		wx.stopPullDownRefresh() //停止下拉刷新
  	}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	  if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
	  this.setData({
	    searchGood: {
	      page: this.data.searchGood.page + 1,
	      size: 10,
	      pages: this.data.searchGood.pages
	    }
	  })
	  	this.getBalanceList()
	  }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 提现
  cashWithdrawal() {
	  if(wx.getStorageSync('userInfo').isRealname==1&&wx.getStorageSync('userInfo').isPayPassword==1){
		  wx.navigateTo({
		    url: '../cashWithdrawal/cashWithdrawal'
		  })
	  }else{
		//   wx.navigateTo({
		//     url: '../personalInfo/personalInfo'
		//   })
		// wx.showToast({
		//   title:'请完善个人信息',
		//   icon: 'none'
		// })  
		wx.showToast({
		    title: '请完善个人信息',
		    icon: 'none',
		    success: function () {
		      setTimeout(function() {
		        wx.navigateTo({
		          url: '../personalInfo/personalInfo'
		        })
		      }, 1000);
		    }
		});
	  }
    
  },
  // 充值
  recharge() {
    wx.navigateTo({
      url: '../recharge/recharge'
    })
  },
  //获取账户记录列表
  getBalanceList() {
    api.post(userBalance.list, {
      uid: this.data.uid||wx.getStorageSync('userInfo').uid,
      type: 1,
      page: this.data.searchGood.page,
      size: this.data.searchGood.size
    }).then(res => {
      // console.log(res)
      let list = res.rows
      list.map(a => {
        a.recordTime = util.getLocalTime(a.recordTime)
        return a
      })
	  let pages = res.total / this.data.searchGood.size
      this.setData({
        dataList: [...this.data.dataList, ...list],
        searchGood: {
          pages: pages,
          page: this.data.searchGood.page,
          size: this.data.searchGood.size,
        }
      })
      // console.log(this.data.dataList)
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  }
})